<?php
package("Utils_Xml");
package("Utils_Resource");
package("Utils_Reflection");
using("Resources_Catalog");
using("Client_User");

class Core_Builder {
	/**
	 * Xml parser
	 *
	 * @var Utils_Xml_Parser
	 */
	private $_xml;
	
	public function __construct() {
		$this->_xml = new Utils_Xml_Parser();
		$resources = new Utils_Resource_Manager($this);
		foreach ($resources as $key) {
			$this->setHandler($key, $resources->getString($key));
		}
	}
	
	/**
	 * @return Core_Class
	 */
	public function build($schema) {
		$stack = $this->_xml->parseFile($schema);
		$class = $stack->pop();
		while ($stack->size() > 1) {
			$class->bindProperty($stack->pop());
		}
		$class->bindKey($stack->pop());
		return $class;
	}
	
	private function setHandler($tag, $handler) {
		try {
			$handle = Utils_Reflection_Facade::getInstance($handler, array());
			$this->_xml->setHandler($tag, $handle);
		} catch (Utils_Reflection_Exception $e) {
			throw new Core_Exception($e);
		}
	}
}
?>